﻿using MediatR;
using SystemHYS.API.Application.command.ProductionRequisitionCommand;
using SystemHYS.Domian;
using SystemHYS.Infrstructre;
using SystemHYS.Infrstructre.Interface;
namespace SystemHYS.API.Application.commandler.ProductionRequisitionCommandler
{
    public class ProductionRequisitionCreateCommandler : IRequestHandler<ProductionRequisitionCreateCommand, int>
    {
        IRawMaterialProductionRepository rawMaterialProductionRepository;
        IProductionRequisitionRepository productionRequisitionRepository;
        public SystemCoretext db;
        public ProductionRequisitionCreateCommandler(IRawMaterialProductionRepository rawMaterialProductionRepository, IProductionRequisitionRepository productionRequisitionRepository,SystemCoretext db)
        {
            this.rawMaterialProductionRepository = rawMaterialProductionRepository;
            this.productionRequisitionRepository = productionRequisitionRepository;
            this.db = db;
        }
        async Task<int> IRequestHandler<ProductionRequisitionCreateCommand, int>.Handle(ProductionRequisitionCreateCommand request, CancellationToken cancellationToken)
        {
            var list = new ProductionRequisition
            {
                requisitionNumber = request.requisitionNumber,
                requisitionName = request.requisitionName,
                requisitionDate = request.requisitionDate.ToUniversalTime(),
                requisitionPerson = request.requisitionPerson,
                workOrderName = request.workOrderName,
                workOrderOrder = request.workOrderOrder,
                taskName = request.taskName,
                taskOrder = request.taskOrder,
                processRoute = request.processRoute,
                processName = request.processName,
                remark = request.remark,
                AddName = "李明",
                AddTime = DateTime.Now.ToUniversalTime()
            };
            await productionRequisitionRepository.AddAsyn(list);
            var id = list.productionRequisitionId;
            foreach (var item in request.rawMaterialList)
            {
                var flist = new RawMaterialProduction
                {
                    RawMaterialId = item.RawMaterialId,
                    productionRequisitionId = id,
                    quantityReceived=item.quantityReceived
                };
                await rawMaterialProductionRepository.AddAsyn(flist);
            }
            return 1;
        }
    }
}
